﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace KeyboardCount
{
    public partial class Form2 : Form
    {
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(
        IntPtr hdcDest, // 目的 DC的句柄
        int nXDest,
        int nYDest,
        int nWidth,
        int nHeight,
        IntPtr hdcSrc, // 源DC的句柄
        int nXSrc,
        int nYSrc,
        System.Int32 dwRop // 光栅的处置数值
        );


        public keys key = null;
        public keys daykey = null;
        public keys todaykey = null;
        public bool IsToday = false;
        public Font CanderFont = null;

        public Form2(keys key, keys todaykey)
        {
            InitializeComponent();
            this.Tag = this.Width.ToString() + "," + this.Height.ToString();

            //根据系统类型放置日历表位置
            System.OperatingSystem OSinfo = Environment.OSVersion;
            int versionMajor = OSinfo.Version.Major;
            if (versionMajor == 5)//5为XP、6为Win7
            {
                monthCalendar1.Left -= 40;
                monthCalendar1.Top += 20;
            }
            CanderFont = monthCalendar1.Font;

            foreach (Control ctrl in this.Controls)
            {
                ctrl.Tag = ctrl.Left.ToString() + "," + ctrl.Top.ToString() + "," + ctrl.Width.ToString() + "," + ctrl.Height.ToString();
            }

            this.key = key;
            this.todaykey = todaykey;
            ShowKeyboard(this.key);
        }

        public void Show()
        {
            if (this.daykey == null)
            {
                ShowKeyboard(this.key);
            }
            else if (IsToday)
            {
                ShowKeyboard(this.todaykey);
            }
        }

        public void ShowKeyboard(keys key)
        {
            label0.Text = key.Digital[0].ToString();
            label1.Text = key.Digital[1].ToString();
            label2.Text = key.Digital[2].ToString();
            label3.Text = key.Digital[3].ToString();
            label4.Text = key.Digital[4].ToString();
            label5.Text = key.Digital[5].ToString();
            label6.Text = key.Digital[6].ToString();
            label7.Text = key.Digital[7].ToString();
            label8.Text = key.Digital[8].ToString();
            label9.Text = key.Digital[9].ToString();
            labelA.Text = key.Letter[0].ToString();
            labelB.Text = key.Letter[1].ToString();
            labelC.Text = key.Letter[2].ToString();
            labelD.Text = key.Letter[3].ToString();
            labelE.Text = key.Letter[4].ToString();
            labelF.Text = key.Letter[5].ToString();
            labelG.Text = key.Letter[6].ToString();
            labelH.Text = key.Letter[7].ToString();
            labelI.Text = key.Letter[8].ToString();
            labelJ.Text = key.Letter[9].ToString();
            labelK.Text = key.Letter[10].ToString();
            labelL.Text = key.Letter[11].ToString();
            labelM.Text = key.Letter[12].ToString();
            labelN.Text = key.Letter[13].ToString();
            labelO.Text = key.Letter[14].ToString();
            labelP.Text = key.Letter[15].ToString();
            labelQ.Text = key.Letter[16].ToString();
            labelR.Text = key.Letter[17].ToString();
            labelS.Text = key.Letter[18].ToString();
            labelT.Text = key.Letter[19].ToString();
            labelU.Text = key.Letter[20].ToString();
            labelV.Text = key.Letter[21].ToString();
            labelW.Text = key.Letter[22].ToString();
            labelX.Text = key.Letter[23].ToString();
            labelY.Text = key.Letter[24].ToString();
            labelZ.Text = key.Letter[25].ToString();
            labelBackSpace.Text = key.BackSpace.ToString();
            labelCaps.Text = key.Caps_lock.ToString();
            labelDelete.Text = key.Delete.ToString();
            labelDenghao.Text = key.denghao.ToString();
            labelDouhao.Text = key.douhao.ToString();
            labelDown.Text = key.DownKey.ToString();
            labelDunhao.Text = key.dunhao.ToString();
            labelEnd.Text = key.End.ToString();
            labelEnter.Text = key.Enter.ToString();
            labelEsc.Text = key.Esc.ToString();
            labelFenhao.Text = key.fenhao.ToString();
            labelHome.Text = key.Home.ToString();
            labelInsert.Text = key.Insert.ToString();
            labelJianhao.Text = key.jianhao.ToString();
            labelJuhao.Text = key.juhao.ToString();
            labelLAlt.Text = key.LAlt.ToString();
            labelLCtrl.Text = key.LCtrl.ToString();
            labelLeft.Text = key.LeftKey.ToString();
            labelLshift.Text = key.LShift.ToString();
            labelMenu.Text = key.caidan.ToString();
            labelPageDown.Text = key.PageDown.ToString();
            labelPageUp.Text = key.PageUp.ToString();
            labelPause.Text = key.PauseBreak.ToString();
            labelPrintSceen.Text = key.PrintScreen.ToString();
            labelRAlt.Text = key.RAlt.ToString();
            labelRCtrl.Text = key.RCtrl.ToString();
            labelRight.Text = key.RightKey.ToString();
            labelRightKey.Text = key.youjian.ToString();
            labelRShift.Text = key.RShift.ToString();
            //labelSceenLock.Text = "";
            labelSpace.Text = key.Space.ToString();
            labelTab.Text = key.TabKey.ToString();
            labelUp.Text = key.UpKey.ToString();
            labelYinhao.Text = key.yinhao.ToString();
            labelYoukuohao.Text = key.youkuohao.ToString();
            labelYouxiexian.Text = key.youxiexian.ToString();
            labelZuokuohao.Text = key.zuokuohao.ToString();
            labelZuoxiexian.Text = key.zuoxiexian.ToString();
        }

        private void linkLabel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (linkLabel1.Text == "缩小")
            {
                linkLabel1.Text = "放大";
                string[] tmp = this.Tag.ToString().Split(',');
                this.Width = (int)(this.Width * 0.7);
                this.Height = (int)(this.Height * 0.7);
                float width = (float)this.Width / (float)Convert.ToInt16(tmp[0]);
                float heigth = (float)this.Height / (float)Convert.ToInt16(tmp[1]);

                this.Tag = this.Width.ToString() + "," + this.Height.ToString();

                foreach (Control ctrl in this.Controls)
                {
                    ctrl.Scale(new SizeF(width, heigth));
                    ctrl.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    ctrl.Top -= 5;
                }

                labelDayRecord.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                monthCalendar1.Font = CanderFont;
                monthCalendar1.Left -= 55;
                monthCalendar1.Top -= 50;

                System.OperatingSystem OSinfo = Environment.OSVersion;
                int versionMajor = OSinfo.Version.Major;
                if (versionMajor == 5)//5为XP、6为Win7
                {
                    monthCalendar1.Left -= 40;
                    monthCalendar1.Top += 20;
                }
            }
            else
            {
                linkLabel1.Text = "缩小";
                this.Width = 1318;
                this.Height = 503;

                this.Tag = this.Width.ToString() + "," + this.Height.ToString();


                foreach (Control ctrl in this.Controls)
                {
                    string[] tmp = ctrl.Tag.ToString().Split(',');
                    
                    ctrl.Width = Convert.ToInt32(tmp[2]);
                    ctrl.Height = Convert.ToInt32(tmp[3]);                    
                    ctrl.Left = Convert.ToInt32(tmp[0]);
                    ctrl.Top = Convert.ToInt32(tmp[1]);
                    ctrl.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                }
                labelDayRecord.Font = new System.Drawing.Font("宋体", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                monthCalendar1.Font = CanderFont;
                if (linkLabel4.Visible)
                {
                    linkLabel3.Top = linkLabel3.Top + 10;
                }
            }
        }

        private void linkLabel2_MouseClick(object sender, MouseEventArgs e)
        {
            saveFileDialog1.Filter = "jpg文件(*.jpg)|*.jpg|bmp文件(*.bmp)|*.bmp";
            saveFileDialog1.Title = "截图";
            saveFileDialog1.FileName = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + " " + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second.ToString();

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Rectangle rect = new Rectangle(this.Left, this.Top, this.Width - 8, this.Height - 28);
                    Graphics g1 = this.CreateGraphics();
                    Image MyImage = new Bitmap(rect.Width, rect.Height, g1);
                    Graphics g2 = Graphics.FromImage(MyImage);
                    IntPtr dc1 = g1.GetHdc();
                    IntPtr dc2 = g2.GetHdc();
                    BitBlt(dc2, 0, 0, rect.Width, rect.Height, dc1, 0, 0, 13369376);
                    g1.ReleaseHdc(dc1);
                    g2.ReleaseHdc(dc2);
                    MyImage.Save(saveFileDialog1.FileName);
                    g1.Dispose();
                    g2.Dispose();
                    MyImage.Dispose();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("截图失败！" + ex.Message);
                }
            }
        }

        private void linkLabel3_MouseClick(object sender, MouseEventArgs e)
        {
            monthCalendar1.Visible = true;
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            monthCalendar1.Visible = false;
            DateTime day = monthCalendar1.SelectionStart;
            this.daykey = new keys();
            string path = Application.StartupPath + "\\DataRecord\\";
            path += day.Year.ToString() + "-" + day.Month.ToString() + "-" + day.Day.ToString() + ".dat";
            if (File.Exists(path))
            {
                DateRecord dateRecord = new DateRecord();
                this.daykey = dateRecord.DeserializeRecord(day, this.daykey);
                labelDayRecord.Text = day.ToLongDateString() + " 按键统计";
                labelDayRecord.Visible = true;
                linkLabel4.Visible = true;
                linkLabel3.Top = linkLabel1.Top + 10;
                if (DateTime.Now.Date == day.Date)
                {
                    IsToday = true;
                    Show();
                }
                else
                {
                    IsToday = false;
                    ShowKeyboard(this.daykey);
                }                
            }
            else
            {
                MessageBox.Show("没有当天的记录！");
            }
        }

        private void linkLabel4_MouseClick(object sender, MouseEventArgs e)
        {
            IsToday = false;
            labelDayRecord.Visible = false;
            linkLabel4.Visible = false;
            linkLabel3.Top = linkLabel1.Top;
            this.daykey = null;
            Show();
        }

        private void Form2_MouseClick(object sender, MouseEventArgs e)
        {
            monthCalendar1.Visible = false;
        }
    }
}
